<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

aos_header( 'Upgrades' );

include( "../global/resources.php" );
?>
					<br />
<?php
$query = "SELECT count(*) FROM upgrade_status WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$upgrades_set = $row[ 'count(*)' ];
$turn = getCurrentTurn();

$something = false;

if( isset( $_SESSION[ 'que_upgrade_status' ] ) ) {
?>
					<div class="blok">
<?php
	print( $_SESSION[ 'que_upgrade_status' ] );
	unset( $_SESSION[ 'que_upgrade_status' ] );
	$something = true;
?>
					</div>
<?php
}

$query = "SELECT * " .
		"FROM upgrade_status " .
		"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND achieved = 0 " .
		"ORDER BY rank";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$num = mysql_numrows( $result );

if( $num > 0 ) {
	if( $something ) {
		print( "<br />" );
	}
	$something = true;
	print "<table>\r\n";
	print "<th colspan=\"5\">" . translate( 'Upgrade queue' ) . "</tr>\r\n";
	print "<tr><td>" . translate( 'Order' ) . "</td><td>" . translate( 'Name' ) . "</td><td>" . translate( 'Time' ) . "</td><td>" . translate( 'Gold cost' ) . "</td><td>" . translate( 'Remove' ) . "</td></tr>\r\n";

	$i = 0;
	while ( $i < $num ) {
		$result2 = mysql_query( "SELECT * " .
				"FROM upgrades " .
				"WHERE upgrade_id = " . mysql_result( $result, $i, "upgrade_id" ) ) or die ( "Query failed: " . mysql_error() );
		$row = mysql_fetch_array($result2, MYSQL_ASSOC);
		if( $i % 2 == 0 ) {
			print "<tr id=\"even\">";
		}
		else {
			print "<tr>";
		}
		$rank = "";
		if( mysql_result( $result, $i, "rank" ) > 0 ) {
			$rank = mysql_result( $result, $i, "rank" );
		}
		
		$level = mysql_result( $result, $i, "level" );
    	$time_cost = $row[ 'time_cost' ];
    	$time_add = $row[ 'time_add' ];
    	$gold_cost = $row[ 'gold_cost' ];
    	$gold_add = $row[ 'gold_add' ];
		
    	if( $row[ 'discount_turn' ] != -1 && $turn > $row[ 'discount_turn' ] ) {
    		$time_cost = round( 0.25 * $time_cost );
    		$time_add = round( 0.25 * $time_add );
    		$gold_cost = round( 0.25 * $gold_cost );
    		$gold_add = round( 0.25 * $gold_add );
    	}
    	
		if( $level > 1 ) {
			$gold_cost = $gold_cost + $gold_add * ( $level - 1 );
			$name = "<a href=\"upgradeinfo.php?id=" . mysql_result( $result, $i, "upgrade_id" ) . "\">" . translate( $row[ "name" ] ) . " " . $level . "</a>";
		}
		else {
			$gold_cost = $gold_cost;
			$name = "<a href=\"upgradeinfo.php?id=" . mysql_result( $result, $i, "upgrade_id" ) . "\">" . translate( $row[ "name" ] ) . "</a>";
		}

		$time = mysql_result( $result, $i, "time" );
		$remove = "";
		if( $time <= 0 ) {
			$time = "";
			$remove = "<a href=\"delete_upgrade.php?rank=" . mysql_result( $result, $i, "rank" ) . "\"><img src=\"../resources/delete.png\" alt=\"" . translate( 'delete from queue' ) . "\" border=\"0\"></a>";
		}
		print "<td align=\"right\">" . $rank . "</td><td>" . $name . "</td><td align=\"right\">" . $time . "</td><td align=\"right\"><strong id=\"gold\">" . number_format( $gold_cost ) . "</strong></td><td align=\"center\">$remove</td></tr>\r\n";
		$i ++;
	}
	print "</table>";
}

$result2 = mysql_query( "SELECT SUM(max_level) " .
		"FROM upgrades" ) or die ( "Query failed: " . mysql_error() );
$row = mysql_fetch_array($result2, MYSQL_ASSOC);
$total_upgrades_available = $row[ 'SUM(max_level)' ];

$query = "SELECT count(*) " .
		"FROM upgrade_status " .
		"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND achieved = 0";
$result2 = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
$total = $row2[ 'count(*)' ];

$query = "SELECT * " .
		"FROM upgrades " .
		"ORDER BY name";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$num = mysql_numrows( $result );

if( $total_upgrades_available - $upgrades_set > 0 && $total < getQueueLength() ) {
	if( $something ) {
		print( "<br/>" );
	}
	$something = true;
	print "<form action=\"que_upgrade.php\" method=\"post\">";
	print "<table>";
	print "<th colspan=\"4\">" . translate( 'Available upgrades' ) . "</th>\r\n";
	print "<tr><td>" . translate( 'Select' ) . "</td><td>" . translate( 'Name' ) . "</td><td>" . translate( 'Gold cost' ) . "</td><td>" . translate( 'Time cost' ) . "</td><tr/>\r\n";

	$i = 0;
	$counter = 0;
	while ( $i < $num ) {
		$result2 = mysql_query( "SELECT count(*) " .
				"FROM upgrade_status " .
				"WHERE upgrade_id = " . mysql_result( $result, $i, "upgrade_id" ) . " AND ruler_id = " . $_SESSION[ 'ruler_id' ] ) or die ( "Query failed: " . mysql_error() );
		$row = mysql_fetch_array($result2, MYSQL_ASSOC);
		if( $row[ 'count(*)' ] == 0 ) {
			$show = true;
		}
		else {
			$show = false;
		}
		
		$result2 = mysql_query( "SELECT count(*) " .
				"FROM upgrade_status " .
				"JOIN upgrades ON upgrades.upgrade_id = upgrade_status.upgrade_id " .
				"WHERE upgrades.upgrade_id = " . mysql_result( $result, $i, "upgrade_id" ) . " AND ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND level = max_level"  ) or die ( "Query failed: " . mysql_error() );
		$row = mysql_fetch_array($result2, MYSQL_ASSOC);
		if( $show || $row[ 'count(*)' ] == 0 ) {
			if( mysql_result( $result, $i, "requires" ) != -1 ) {
				$result2 = mysql_query( "SELECT count(*) " .
						"FROM upgrade_status " .
						"WHERE upgrade_id = " . mysql_result( $result, $i, "requires" ) . " AND ruler_id = " . $_SESSION[ 'ruler_id' ]  ) or die ( "Query failed: " . mysql_error() );
				$row = mysql_fetch_array($result2, MYSQL_ASSOC);
			}
			//echo " total " . $row[ 'count(*)' ] . " ";
			if( mysql_result( $result, $i, "requires" ) == -1 || $row[ 'count(*)' ] == 1 ) {
				if( $counter % 2 == 0 ) {
					print "<tr id=\"even\">";
				}
				else {
					print "<tr>";
				}
				$result2 = mysql_query( "SELECT MAX(level) " .
						"FROM upgrade_status " .
						"WHERE upgrade_id = " . mysql_result( $result, $i, "upgrade_id" ) . " AND ruler_id = " . $_SESSION[ 'ruler_id' ]  ) or die ( "Query failed: " . mysql_error() );
				$row = mysql_fetch_array($result2, MYSQL_ASSOC);
				$current_level = $row[ 'MAX(level)' ];
		    	
		    	$time_cost = mysql_result( $result, $i, "time_cost" );
		    	$time_add = mysql_result( $result, $i, "time_add" );
		    	$gold_cost = mysql_result( $result, $i, "gold_cost" );
		    	$gold_add = mysql_result( $result, $i, "gold_add" );
				
		    	if( mysql_result( $result, $i, "discount_turn" ) != -1 && $turn > mysql_result( $result, $i, "discount_turn" ) ) {
		    		$time_cost = round( 0.25 * $time_cost );
		    		$time_add = round( 0.25 * $time_add );
		    		$gold_cost = round( 0.25 * $gold_cost );
		    		$gold_add = round( 0.25 * $gold_add );
		    	}
				
				if( $current_level > 0 ) {
					$name = "<a href=\"upgradeinfo.php?id=" . mysql_result( $result, $i, "upgrade_id" ) . "\">" . translate( mysql_result( $result, $i, "name" ) ) . " " . ( $current_level + 1 ) . "</a>";
					$gold_cost = $gold_cost + $gold_add * $current_level;
					$time_cost = $time_cost + $time_add * $current_level;
				}
				else {
					$name = "<a href=\"upgradeinfo.php?id=" . mysql_result( $result, $i, "upgrade_id" ) . "\">" . translate( mysql_result( $result, $i, "name" ) ) . "</a>";
				}
				
				print( "<td><input type=\"checkbox\" name=\"" . mysql_result( $result, $i, "upgrade_id" ) . "\" /></td>" .
						"<td>" . $name . "</td>" .
						"<td align=\"right\"><strong class=\"gold\">" . number_format( $gold_cost ) . "</strong></td>" .
						"<td align=\"right\">" . number_format( $time_cost ) . "</td>" . "</tr>\r\n" );
				$counter ++;
			}
		}
		$i ++;
	}
	mysql_free_result( $result );
	print( "</table>" );
	print( "<input type=\"submit\" value=\"" . translate( 'add to queue' ) . "\" />\r\n" );
	print( "</form>\r\n" );
}

$query = "SELECT upgrade_status.upgrade_id, level " .
		"FROM upgrade_status " .
		"JOIN upgrades ON upgrades.upgrade_id = upgrade_status.upgrade_id " .
		"WHERE upgrade_status.ruler_id = " . $_SESSION[ 'ruler_id' ]. " AND upgrade_status.achieved = 1 " .
		"ORDER BY upgrades.name, level";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$num = mysql_numrows( $result );

if( $num > 0 ) {
	if( $something ) {
		print( "<br /><br />" );
	}
	print "<table>";
	print "<th colspan=\"2\">" . translate( 'Applied upgrades' ) . "</th>\r\n";
	print "<tr><td>" . translate( 'Name' ) . "</td><td>" . translate( 'Description' ) . "</td><tr/>\r\n";

	$i = 0;
	while ( $i < $num ) {
		if( $i % 2 == 0 ) {
			print "<tr id=\"even\">";
		}
		else {
			print "<tr>";
		}
		$result2 = mysql_query( "SELECT name, description " .
				"FROM upgrades " .
				"WHERE upgrade_id = " . mysql_result( $result, $i, "upgrade_id" ) ) or die ( "Query failed: " . mysql_error() );
		$row = mysql_fetch_array($result2, MYSQL_ASSOC);
		
		if( mysql_result( $result, $i, "level" ) > 1 ) {
			$name = "<a href=\"upgradeinfo.php?id=" . mysql_result( $result, $i, "upgrade_id" ) . "\">" . translate( $row[ 'name' ] ) . " " . mysql_result( $result, $i, "level" ) . "</a>";
		}
		else {
			$name = "<a href=\"upgradeinfo.php?id=" . mysql_result( $result, $i, "upgrade_id" ) . "\">" . translate( $row[ 'name' ] ) . "</a>";
		}

		print "<td>" . $name . "</td><td>" . translate( $row[ 'description' ] ) . "</td></tr>\r\n";
		$i ++;
	}

	mysql_free_result( $result );
	print "</table>";
}
aos_footer();
?>